package com.treeroot.treenet.enums;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * Description: 编码类型
 * Create By TreeRoot
 * Create Time: 2025/06/02 07:53
 * Class Name: EncodingType
 *
 * @author TreeRoot
 */
public enum TcpEncodingType {
    UTF8(1, StandardCharsets.UTF_8),
    GBK(2, Charset.forName("GBK")),

    // JSON一般就是UTF-8编码的字符串
    JSON(3, StandardCharsets.UTF_8);

    private final int code;
    private final Charset charset;

    TcpEncodingType(int code, Charset charset) {
        this.code = code;
        this.charset = charset;
    }

    public int getCode() {
        return code;
    }

    public Charset getCharset() {
        return charset;
    }

    public static TcpEncodingType fromCode(int code) {
        for (TcpEncodingType type : values()) {
            if (type.code == code) {
                return type;
            }
        }
        throw new IllegalArgumentException("未知编码类型: " + code);
    }
}